<?php

/**
 * Created by PhpStorm.
 * User: dumbledore
 * Date: 2017/12/25
 * Time: 上午10:12
 */
class Test extends CI_Controller
{
    public function __construct(){
        parent::__construct();
        $this->load->database();
        $this->load->library('form_validation');//加载表单验证类库
        $this->load->helper('json');
        $this->load->helper('common');
        $this->load->library('session');
        $this->output->set_content_type('application/html;charset=utf-8');
    }

    // 登录demo
    public function login_api()
    {
        $post = $this->input->post();
        $callback = $_GET['callback'];
//        if(check_param($post,['username','password']))
//        {
            // 校验密码

            // 存入session
            $this->session->set_userdata('shop_user_id', '9938');
            $session_id = session_id(); // 测试用
            $data = ['code'=>200,'shop_user_id'=>9938,'session_id'=>$session_id];
            echo $callback.'('.json_encode($data).')';
            exit;
//        }

    }

    // 注销demo
    public function logout_api()
    {
        $callback = $_GET['callback'];
            if($this->session->shop_user_id)
            {
                $this->session->unset_userdata(['shop_user_id']);
                $data = ['code'=>200,'data'=>'','msg'=>'注销成功'];
                echo $callback.'('.json_encode($data).')';
                exit;
            }else{
                $data = ['code'=>200,'data'=>'','msg'=>'尚未注销'];
                echo $callback.'('.json_encode($data).')';
                exit;
            }

    }

    // 测试登录demo
    public function test_login()
    {
        if($this->session->shop_user_id)
        {
            $shop_user_id = $this->session->shop_user_id;
            $session_id = session_id(); // 测试用
            send_json(array('code'=>0,'data'=>['shop_user_id'=>$shop_user_id,'session_id'=>$session_id],'msg'=>"注销成功"));
        }else{
            send_json(array('code'=>-1,'data'=>'','msg'=>"尚未登录"));
        }
    }
}